package com.totoro.mongo.repository;

import com.alibaba.fastjson.JSONObject;
import com.mongodb.*;
import com.mongodb.operation.UpdateOperation;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.MatchOperation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Date;

@Repository
public class DemoRepository {

    @Resource
    private MongoTemplate mongoTemplate;

    public void findByTitle(String title) {

//        DBCursor cursor = mongoTemplate.getCollection("movies").find((DBObject) new BasicDBObject().put("title",title)) ;
        DBCursor cursor = mongoTemplate.getCollection("movies").find();

        while (cursor.hasNext()) {

            DBObject object = cursor.next();
            System.out.println(object.get("year"));
        }
    }

    public void findAllCollectionData(String collectionName) {
        DBCursor cursor = mongoTemplate.getCollection(collectionName).find();
        while (cursor.hasNext()) {
            DBObject object = cursor.next();
            System.out.println(object.get("_id"));
            String json = JSONObject.toJSONString(object.toMap(), true);
            System.out.println(json);
        }
    }

    public void getDBCollection(String collectionName) {
        DBCollection collection = mongoTemplate.getCollection(collectionName);
        boolean exists = mongoTemplate.collectionExists(collectionName);
        System.out.println(exists);
        System.out.println(collection.getName());
    }

    public void createCollection(String collectonName) {
        DBCollection collection = mongoTemplate.createCollection(collectonName);
        System.out.println(collection.getName());
        System.out.println(collection.getFullName());
    }

    public void copyCollection(String orgCollection, String destCollection) {
        DBCursor cursor = mongoTemplate.getCollection(orgCollection).find();
        DBCollection $desCollection = mongoTemplate.getCollection(destCollection);
        while (cursor.hasNext()) {
            $desCollection.insert(cursor.next());
        }
    }

    public void aggregate() {
        MatchOperation matchOperation = new MatchOperation(new Criteria());
//        mongoTemplate.getCollection("").aggregate()
    }

    /**
     * 测试找到并修改，如果没有是否会创建
     */
    public void testFindAndModifyOrCreate() {
        DBObject query = new BasicDBObject();
        query.put("_id", "012345678");


        DBObject modify = new BasicDBObject();
        modify.put("query_skip", 90000);
        modify.put("update_time", new Date());
        //mongoTemplate.getCollection("people_temp").findAndModify(query, modify) ;

        mongoTemplate.getCollection("people_temp").update(query, modify, true, false);

    }

    public void gtId() {
        DBObject query = new BasicDBObject();

        query.put("_id", new BasicDBObject("$gte", "57d7a18cfa937f710a7e0f7d"));
        DBCursor cursor = mongoTemplate.getCollection("people_temp").find(query);

        System.out.println(cursor.count());

        DBObject queryObject = new BasicDBObject().append("_id",
                new BasicDBObject().append(QueryOperators.GT, new ObjectId("57d7a18bfa937f710a7e0f41")));


        DBCursor cursor2 = mongoTemplate.getCollection("people_temp").find(queryObject);
        System.out.println(cursor2.count());

        //Criteria criteria = Criteria.where("_id").gt(new ObjectId("57d7a18cfa937f710a7e0f7d")) ;
    }

    public void testUpdate() {
        DBCollection collection = mongoTemplate.getCollection("people_temp");
        DBObject query = new BasicDBObject();
        query.put("_id", "012345678");

        DBObject update = new BasicDBObject();
        update.put("query_skip",10) ;
        update.put("query_updateTime",new Date()) ;

        //collection.update(query, update, true ,false) ;
        collection.findAndModify(query, update) ;
       // collection.update(query, update) ;
    }

    public void testUpdate2(){
        DBCollection collection = mongoTemplate.getCollection("people_temp");
        DBObject query = new BasicDBObject();
        query.put("_id", "012345678");

        DBObject update = new BasicDBObject().append("$set",new BasicDBObject("query_skip",100)) ;

        collection.update(query,update) ;
    }

}
